﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SemiCASE.Business;
using System.Data;

public partial class PopUp_CreateUseCase : SemiCASEBasePage
{
    private string ProjectID
    {
        get
        {
            return Request.QueryString["ProjectID"] != null ? Request.QueryString["ProjectID"] : String.Empty;
        }
    }

    private string UseCaseID
    {
        get
        {
            return Request.QueryString["UseCaseID"] != null ? Request.QueryString["UseCaseID"] : String.Empty;
        }
    }

    private string ScreenMode
    {
        get
        {
            return Request.QueryString["ScreenMode"] != null ? Request.QueryString["ScreenMode"] : String.Empty;
        }
    }


    private Dictionary<string, string> OrderSign
    {
        get
        {
            Dictionary<string, string> orderSign = new Dictionary<string, string>();
            orderSign.Add("1", "a");
            orderSign.Add("2", "b");
            orderSign.Add("3", "c");
            orderSign.Add("4", "d");
            orderSign.Add("5", "e");
            orderSign.Add("6", "f");
            orderSign.Add("7", "g");
            orderSign.Add("8", "h");
            orderSign.Add("9", "i");
            orderSign.Add("10", "j");
            return orderSign;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Session["MSS"] = null;
            Session["Extentions"] = null;
            Session["ExtentionSteps"] = null;
            LoadStatus();
            SetScreenMode();
        }
    }

    private void SetScreenMode()
    {
        if (this.ScreenMode == "Display")
        {
            ThBaslik.InnerText = "USE CASE INFO";
            LinkButttonAddExtention.Visible = false;
            LinkButtonAddMSS.Visible = false;
            GenelTextBoxMSS.Visible = false;
            gvMSS.Columns[2].Visible = false;
            gvMSS.Columns[3].Visible = false;
            gvMSS.Columns[4].Visible = false;
            gvExtention.Columns[4].Visible = false;
            gvExtention.Columns[5].Visible = false;
            PanelAddExtension.Visible = false;
            ButtonSave.Visible = false;
            LoadUseCaseInfo();
            LoadMSSFromDB();
            LoadExtentionStepsFromDB();
            LoadExtentionsFromDB();
            SetFunctionalNonFunctionalReq();
        }
        else if (this.ScreenMode == "Create")
        {
            ThBaslik.InnerText = "CREATE USE CASE";
            LoadMSS();
            LoadExtentions();
        }
        else
        {
            ThBaslik.InnerText = "UPDATE USE CASE";
            LoadUseCaseInfo();
            LoadMSSFromDB();
            LoadExtentionStepsFromDB();
            LoadExtentionsFromDB();
            SetFunctionalNonFunctionalReq();
        }
    }

    private void SetFunctionalNonFunctionalReq()
    {
        DataTable dt = (DataTable)Session["UseCaseList"];
        if (dt == null || dt.Rows.Count == 0)
            return;

        foreach (DataRow row in dt.Rows)
        {
            if (row["UseCaseID"].ToString() == this.UseCaseID)
            {
                if (row["Nonfunctional"] != DBNull.Value)
                {
                    if (Convert.ToBoolean(row["Nonfunctional"]))
                    {
                        RadioButtonNonFunctional.Checked = true;
                        RadioButtonFunctionalReq.Checked = false;
                        TableFunctional.Visible = false;
                    }
                    else
                    {
                        RadioButtonFunctionalReq.Checked = true;
                        RadioButtonNonFunctional.Checked = false;
                        TableFunctional.Visible = true;
                    }
                }
            }
        }
    }

    private void LoadUseCaseInfo()
    {
        DataTable dt = (DataTable)Session["UseCaseList"];
        if (dt == null || dt.Rows.Count == 0)
            return;

        foreach (DataRow row in dt.Rows)
        {
            if (row["UseCaseID"].ToString() == this.UseCaseID)
            {
                GenelTextBoxFrequency.Text = row["Frequency"] != DBNull.Value ? row["Frequency"].ToString() : String.Empty;
                GenelTextBoxUseCaseName.Text = row["Name"] != DBNull.Value ? row["Name"].ToString() : String.Empty;
                GenelTextBoxScope.Text = row["Scope"] != DBNull.Value ? row["Scope"].ToString() : String.Empty;
                GenelTextBoxLevel.Text = row["Level"] != DBNull.Value ? row["Level"].ToString() : String.Empty;
                GenelTextBoxPrimaryActor.Text = row["PrimaryActor"] != DBNull.Value ? row["PrimaryActor"].ToString() : String.Empty;
                GenelTextBoxStakeHolders.Text = row["StakeHolders"] != DBNull.Value ? row["StakeHolders"].ToString() : String.Empty;
                GenelTextBoxPreconditions.Text = row["Precondition"] != DBNull.Value ? row["Precondition"].ToString() : String.Empty;
                GenelTextBoxSuccessGuarantees.Text = row["SuccessGuarantee"] != DBNull.Value ? row["SuccessGuarantee"].ToString() : String.Empty;
                GenelTextBoxSpecialRequirements.Text = row["SpecialRequirement"] != DBNull.Value ? row["SpecialRequirement"].ToString() : String.Empty;
                GenelTextBoxTechnology.Text = row["Technology"] != DBNull.Value ? row["Technology"].ToString() : String.Empty;
                GenelTextBoxMisc.Text = row["Miscelleneous"] != DBNull.Value ? row["Miscelleneous"].ToString() : String.Empty;
                if (row["StatusID"] != DBNull.Value)
                    (DropDownListStatus.Items.FindByValue(row["StatusID"].ToString())).Selected = true;
                if (row["Priority"] != DBNull.Value)
                    (DropDownListPriority.Items.FindByValue(row["Priority"].ToString())).Selected = true;
                break;
            }
        }
    }

    private void LoadMSSFromDB()
    {
        string p = this.ProjectID;
        string u = this.UseCaseID;
        if (String.IsNullOrEmpty(p) || String.IsNullOrEmpty(u))
            return;

        DataTable dt = new MainSuccessBS().LoadMSSFromDB(new Guid(p), new Guid(u));
        Session["MSS"] = dt;
        LoadMSS();
    }

    private void LoadExtentionStepsFromDB()
    {
        string p = this.ProjectID;
        string u = this.UseCaseID;
        if (String.IsNullOrEmpty(p) || String.IsNullOrEmpty(u))
            return;

        DataTable dt = new ExtentionStepBS().LoadExtentionStepsFromDB(new Guid(p), new Guid(u));
        Session["ExtentionSteps"] = dt;
        LoadExtentions();
    }

    private void LoadExtentionsFromDB()
    {
        string p = this.ProjectID;
        string u = this.UseCaseID;
        if (String.IsNullOrEmpty(p) || String.IsNullOrEmpty(u))
            return;

        DataTable dt = new ExtentionBS().LoadExtentionsFromDB(new Guid(p), new Guid(u));
        Session["Extentions"] = dt;
        LoadExtentions();
    }

    private void LoadStatus()
    {
        DataTable dt = new UseCaseBS().LoadStatus();
        if (dt.Rows.Count > 0)
        {
            DropDownListStatus.DataSource = dt;
            DropDownListStatus.DataBind();
        }
        else
        {
            DropDownListStatus.DataSource = null;
            DropDownListStatus.DataBind();
        }
    }

    private void LoadExtentions()
    {
        DataTable dt = (DataTable)Session["Extentions"];
        if (dt == null)
        {
            gvExtention.DataSource = null;
            gvExtention.DataBind();
        }
        else
        {
            if (dt.Rows.Count > 0)
            {
                dt.DefaultView.Sort = "MssSiraNo ASC, OrderSign ASC";
                gvExtention.DataSource = dt.DefaultView.ToTable();
                gvExtention.DataBind();
            }
            else
            {
                gvExtention.DataSource = null;
                gvExtention.DataBind();
            }
        }
    }

    private void LoadMSS()
    {
        DataTable dt = (DataTable)Session["MSS"];
        if (dt == null)
        {
            gvMSS.DataSource = null;
            gvMSS.DataBind();
        }
        else
        {
            if (dt.Rows.Count > 0)
            {
                dt.DefaultView.Sort = "SiraNo ASC";
                gvMSS.DataSource = dt.DefaultView.ToTable();
                gvMSS.DataBind();
                Session["MSS"] = dt.DefaultView.ToTable();
            }
            else
            {
                gvMSS.DataSource = null;
                gvMSS.DataBind();

            }
        }
    }

    protected void gvExtention_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "DeleteExtention")
        {
            DeleteExtention(e.CommandArgument.ToString());
        }
        else if (e.CommandName == "AddStep")
        {
            HiddenFieldExtentionID.Value = e.CommandArgument.ToString();
            MPES.Show();
        }
    }

    protected void gvMSS_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "DeleteMSS")
        {
            DeleteMSS(e.CommandArgument.ToString());
        }
        else if (e.CommandName == "AddExtention")
        {
            HiddenFieldMSSID.Value = e.CommandArgument.ToString();
            LabelUyariEx.Text = String.Empty;
            MPE.Show();
        }
        else if (e.CommandName == "UpMss")
        {
            UpMss(Convert.ToInt32(e.CommandArgument.ToString()));
        }
        else if (e.CommandName == "DownMss")
        {
            DownMss(Convert.ToInt32(e.CommandArgument.ToString()));
        }
    }

    private void DownMss(int pSiraNo)
    {
        DataTable dt = (DataTable)Session["MSS"];
        if (pSiraNo == dt.Rows.Count)
            return;

        dt.Rows[pSiraNo - 1]["SiraNo"] = pSiraNo + 1;
        dt.Rows[pSiraNo]["SiraNo"] = pSiraNo;
        Session["MSS"] = dt;
        SetExtentionSiraNo();
        LoadMSS();
    }

    private void UpMss(int pSiraNo)
    {
        DataTable dt = (DataTable)Session["MSS"];
        if (pSiraNo == 1)
            return;

        dt.Rows[pSiraNo - 1]["SiraNo"] = pSiraNo - 1;
        dt.Rows[pSiraNo - 2]["SiraNo"] = pSiraNo;
        Session["MSS"] = dt;
        SetExtentionSiraNo();
        LoadMSS();
    }

    private void SetExtentionSiraNo()
    {
        DataTable dt = (DataTable)Session["MSS"];
        DataTable dtExt = (DataTable)Session["Extentions"];

        if (dt == null || dtExt == null)
            return;

        for (int k = 0; k < dt.Rows.Count; k++)
        {
            if (dtExt != null)
            {
                for (int m = 0; m < dtExt.Rows.Count; m++)
                {
                    if (dt.Rows[k]["MssID"].ToString() == dtExt.Rows[m]["MssID"].ToString())
                        dtExt.Rows[m]["MssSiraNo"] = dt.Rows[k]["SiraNo"].ToString();
                }
            }
        }
        Session["Extentions"] = dtExt;
        LoadExtentions();
    }

    protected void gvMSS_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType != DataControlRowType.DataRow)
            return;
    }

    protected void gvExtention_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType != DataControlRowType.DataRow)
            return;

        DataTable dt = (DataTable)Session["ExtentionSteps"];

        if (dt == null)
            return;

        GridView gvExtentionStep = (GridView)e.Row.FindControl("gvExtentionStep");

        DataRow[] rows = dt.Select("ExtentionID='" + gvExtention.DataKeys[e.Row.RowIndex].Value + "'");
        DataTable dtTemp = dt.Clone();

        foreach (DataRow r in rows)
        {
            dtTemp.Rows.Add(r[0], r[1], r[2], r[3]);
        }
        dtTemp.AcceptChanges();

        gvExtentionStep.DataSource = dtTemp;
        gvExtentionStep.DataBind();
    }

    protected void gvExtentionStep_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "DeleteStep")
        {
            DeleteStep(e.CommandArgument.ToString());
        }
    }

    private void DeleteStep(string p)
    {
        string extentionID = String.Empty;
        DataTable dt = (DataTable)Session["ExtentionSteps"];

        foreach (DataRow row in dt.Rows)
        {
            if (row["StepID"].ToString() == p)
            {
                extentionID = row["ExtentionID"].ToString();
                row.Delete();
                break;
            }
        }
        dt.AcceptChanges();

        int k = 1;
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            if (dt.Rows[i]["ExtentionID"].ToString() == extentionID)
            {
                dt.Rows[i]["SiraNo"] = k;
                k++;
            }
        }
        Session["ExtentionSteps"] = dt;
        LoadExtentions();
    }

    private void AddExtentionToMSS()
    {
        DataTable dtMSS = (DataTable)Session["MSS"];
        if (dtMSS == null || dtMSS.Rows.Count == 0)
        {
            LabelUyariEx.Text = "First add Mss!";
            MPE.Show();
            return;
        }

        if (String.IsNullOrEmpty(GenelTextBoxExtention.Text))
        {
            LabelUyariEx.Text = "Extention blank!";
            MPE.Show();
            return;
        }

        LabelUyariEx.Text = String.Empty;
        DataTable dt = (DataTable)Session["Extentions"];
        if (dt == null)
        {
            dt = new DataTable();
            dt.Columns.Add("SiraNo", Type.GetType("System.String"));
            dt.Columns.Add("MssSiraNo", Type.GetType("System.String"));
            dt.Columns.Add("ExtentionID", Type.GetType("System.Guid"));
            dt.Columns.Add("MssID", Type.GetType("System.Guid"));
            dt.Columns.Add("Name", Type.GetType("System.String"));
            dt.Columns.Add("Steps", Type.GetType("System.String"));
            dt.Columns.Add("OrderSign", Type.GetType("System.String"));
            dt.AcceptChanges();
        }
        DataRow row = dt.NewRow();

        if (HiddenFieldMSSID.Value == String.Empty)
        {
            row["MssSiraNo"] = "*";
        }
        else
        {
            for (int i = 0; i < dtMSS.Rows.Count; i++)
            {
                if (dtMSS.Rows[i]["MssID"].ToString() == HiddenFieldMSSID.Value)
                {
                    row["MssSiraNo"] = dtMSS.Rows[i]["SiraNo"];
                    break;
                }
            }
        }

        if (HiddenFieldMSSID.Value == String.Empty)
            row["MssID"] = DBNull.Value;
        else
            row["MssID"] = HiddenFieldMSSID.Value;
        row["ExtentionID"] = Guid.NewGuid();
        row["Name"] = GenelTextBoxExtention.Text;

        //OrderSign ayarla
        dt.DefaultView.Sort = "MssSiraNo ASC, OrderSign ASC";
        DataRow[] rows = dt.Select("MssSiraNo='" + row["MssSiraNo"].ToString() + "'");

        if (rows.Length == 0)
        {
            row["SiraNo"] = "1";
            row["OrderSign"] = this.OrderSign["1"];
        }
        else
        {
            int i = 0;
            for (int k = 0; k < rows.Length; k++)
            {
                if (i < Convert.ToInt32(rows[k]["SiraNo"].ToString()))
                    i = Convert.ToInt32(rows[k]["SiraNo"].ToString());
            }

            row["SiraNo"] = i + 1;
            row["OrderSign"] = this.OrderSign[(i + 1).ToString()];
        }

        dt.Rows.Add(row);
        dt.AcceptChanges();
        Session["Extentions"] = dt;
        GenelTextBoxExtention.Text = String.Empty;
        HiddenFieldMSSID.Value = String.Empty;
        LoadExtentions();
    }

    private void AddExtentionStep()
    {
        DataTable dt = (DataTable)Session["ExtentionSteps"];
        if (dt == null)
        {
            dt = new DataTable();
            dt.Columns.Add("StepID", Type.GetType("System.Guid"));
            dt.Columns.Add("ExtentionID", Type.GetType("System.Guid"));
            dt.Columns.Add("SiraNo", Type.GetType("System.String"));
            dt.Columns.Add("StepName", Type.GetType("System.String"));
            dt.AcceptChanges();
        }

        DataRow row = dt.NewRow();

        row["StepID"] = Guid.NewGuid();
        row["ExtentionID"] = HiddenFieldExtentionID.Value;
        DataRow[] rows = dt.Select("ExtentionID='" + HiddenFieldExtentionID.Value + "'");
        row["SiraNo"] = (rows.Length + 1).ToString();
        row["StepName"] = GenelTextBoxStep.Text;
        dt.Rows.Add(row);
        dt.AcceptChanges();
        Session["ExtentionSteps"] = dt;
        GenelTextBoxStep.Text = String.Empty;
        HiddenFieldExtentionID.Value = String.Empty;
        LoadExtentions();
    }

    private void DeleteExtention(string extentionID)
    {
        DataTable dt = (DataTable)Session["Extentions"];
        if (dt == null)
            return;
        string mssSiraNo = String.Empty;
        int counter = 1;
        foreach (DataRow row in dt.Rows)
        {
            if (row["ExtentionID"].ToString() == extentionID)
            {
                mssSiraNo = row["MssSiraNo"].ToString();
                row.Delete();
                break;
            }
        }
        dt.AcceptChanges();

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            if (dt.Rows[i]["MssSiraNo"].ToString() == mssSiraNo)
            {
                dt.Rows[i]["SiraNo"] = counter.ToString();
                dt.Rows[i]["OrderSign"] = this.OrderSign[counter.ToString()];
                counter++;
            }
        }
        dt.AcceptChanges();
        Session["Extentions"] = dt;
        LoadExtentions();
    }

    protected void LinkButtonAddExtention_Click(object sender, EventArgs e)
    {
        AddExtentionToMSS();
    }

    protected void LinkButtonAddStep_Click(object sender, EventArgs e)
    {
        AddExtentionStep();
    }

    protected void LinkButtonAddMSS_Click(object sender, EventArgs e)
    {
        AddMSS();
    }

    private void DeleteMSS(string mssID)
    {
        DataTable dt = (DataTable)Session["MSS"];
        DataTable dtExt = (DataTable)Session["Extentions"];
        DataTable dtExtSteps = (DataTable)Session["ExtentionSteps"];

        if (dt == null)
            return;

        foreach (DataRow row in dt.Rows)
        {
            if (row["MssID"].ToString() == mssID)
            {
                row.Delete();

                if (dtExt != null)
                {
                    foreach (DataRow rowExt in dtExt.Rows)
                    {
                        if (rowExt["MssID"].ToString() == mssID)
                        {
                            if (dtExtSteps != null)
                            {
                                foreach (DataRow rowExtStep in dtExtSteps.Rows)
                                {
                                    if (rowExtStep["ExtentionID"].ToString() == rowExt["ExtentionID"].ToString())
                                        rowExtStep.Delete();
                                }
                                dtExtSteps.AcceptChanges();
                            }
                            rowExt.Delete();
                        }
                    }
                    dtExt.AcceptChanges();
                }

                break;
            }
        }
        dt.AcceptChanges();

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            dt.Rows[i]["SiraNo"] = (i + 1).ToString();
        }

        for (int k = 0; k < dt.Rows.Count; k++)
        {
            if (dtExt != null)
            {
                for (int m = 0; m < dtExt.Rows.Count; m++)
                {
                    if (dt.Rows[k]["MssID"].ToString() == dtExt.Rows[m]["MssID"].ToString())
                        dtExt.Rows[m]["MssSiraNo"] = dt.Rows[k]["SiraNo"].ToString();
                }
            }
        }


        Session["MSS"] = dt;
        LoadMSS();
        LoadExtentions();
    }

    private void AddMSS()
    {
        if (String.IsNullOrEmpty(GenelTextBoxMSS.Text))
        {
            LabelUyari.Text = "MSS blank!";
            return;
        }

        LabelUyariEx.Text = String.Empty;
        GenelTextBoxExtention.Text = String.Empty;
        LabelUyari.Text = String.Empty;
        DataTable dt = (DataTable)Session["MSS"];
        if (dt == null)
        {
            dt = new DataTable();
            dt.Columns.Add("SiraNo", Type.GetType("System.String"));
            dt.Columns.Add("MssID", Type.GetType("System.Guid"));
            dt.Columns.Add("Name", Type.GetType("System.String"));
            dt.AcceptChanges();
        }

        DataRow row = dt.NewRow();
        row["SiraNo"] = dt.Rows.Count + 1;
        row["MssID"] = Guid.NewGuid();
        row["Name"] = GenelTextBoxMSS.Text;
        dt.Rows.Add(row);
        dt.AcceptChanges();
        Session["MSS"] = dt;
        GenelTextBoxMSS.Text = String.Empty;
        LoadMSS();
    }

    protected void ButtonSave_Click(object sender, EventArgs e)
    {
        if (this.ScreenMode == "Create")
            CreateUseCase();
        else if (this.ScreenMode == "Update")
        {
            UpdateUseCase();
        }
    }

    private void UpdateUseCase()
    {
        new UseCaseBS().UpdateUseCase(new Guid(this.ProjectID), new Guid(this.UseCaseID));
        CreateUseCase();
        JavascriptHelper.PopUpiKapatAcanPencereyiRefresh();
    }

    private void CreateUseCase()
    {
        if (RadioButtonFunctionalReq.Checked)
        {
            if (DropDownListPriority.SelectedItem.Value == "0")
            {
                MessageBox("Please select priortiy", SemiCASE.Interfaces.MesajTuruEnum.Uyari);
                return;
            }

            if (GenelTextBoxUseCaseName.Text == "")
            {
                MessageBox("Please enter use case name", SemiCASE.Interfaces.MesajTuruEnum.Uyari);
                return;
            }

            new UseCaseBS().CreateUseCase(Guid.NewGuid(),
                                        new Guid(this.ProjectID),
                                        GenelTextBoxFrequency.Text,
                                        GenelTextBoxLevel.Text,
                                        GenelTextBoxMisc.Text,
                                        GenelTextBoxPreconditions.Text,
                                        GenelTextBoxPrimaryActor.Text,
                                        GenelTextBoxScope.Text,
                                        GenelTextBoxSpecialRequirements.Text,
                                        GenelTextBoxStakeHolders.Text,
                                        GenelTextBoxSuccessGuarantees.Text,
                                        GenelTextBoxTechnology.Text,
                                        GenelTextBoxUseCaseName.Text,
                                        Convert.ToInt32(DropDownListStatus.SelectedItem.Value),
                                        (DataTable)Session["MSS"],
                                        (DataTable)Session["Extentions"],
                                        (DataTable)Session["ExtentionSteps"],
                                        DateTime.Now,
                                        DropDownListPriority.SelectedItem.Value);
        }
        else if (RadioButtonNonFunctional.Checked)
        {
            new UseCaseBS().CreateNonFunctionalUseCase(new Guid(this.ProjectID), Guid.NewGuid(), GenelTextBoxUseCaseName.Text, 1);
        }
        JavascriptHelper.PopUpiKapatAcanPencereyiRefresh();
    }

    protected void RadioButtonFunctionalReq_CheckedChanged(object sender, EventArgs e)
    {
        TableFunctional.Visible = true;
    }

    protected void RadioButtonNonFunctional_CheckedChanged(object sender, EventArgs e)
    {
        TableFunctional.Visible = false;
    }

    protected void gvMSS_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvMSS.EditIndex = e.NewEditIndex;
        LoadMSS();
    }

    protected void gvMSS_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvMSS.EditIndex = -1;
        LoadMSS();
    }

    protected void gvExtention_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvExtention.EditIndex = e.NewEditIndex;
        LoadExtentions();
    }

    protected void gvExtention_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvExtention.EditIndex = -1;
        LoadExtentions();
    }

    protected void gvMSS_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        if (e.RowIndex < 0)
            return;

        DataTable dt = (DataTable)Session["MSS"];
        dt.Rows[e.RowIndex]["Name"] = e.NewValues[0].ToString();

        gvMSS.EditIndex = -1;
        Session["MSS"] = dt;
        LoadMSS();
    }

    protected void gvExtention_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        if (e.RowIndex < 0)
            return;

        DataTable dt = (DataTable)Session["Extentions"];
        TextBox txtAdi = gvExtention.Rows[e.RowIndex].FindControl("TextBoxExtention") as TextBox;
        dt.Rows[e.RowIndex]["Name"] = txtAdi.Text;

        gvExtention.EditIndex = -1;
        Session["Extentions"] = dt;
        LoadExtentions();
    }

    protected void gvExtentionStep_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView gv = (GridView)sender;
        gv.EditIndex = e.NewEditIndex;
        LoadExtentions();
    }

    protected void gvExtentionStep_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {

        LoadExtentions();
    }

    protected void gvExtentionStep_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        if (e.RowIndex < 0)
            return;

        DataTable dt = (DataTable)Session["Extentions"];
        TextBox txtAdi = gvExtention.Rows[e.RowIndex].FindControl("TextBoxExtention") as TextBox;
        dt.Rows[e.RowIndex]["Name"] = txtAdi.Text;

        gvExtention.EditIndex = -1;
        Session["Extentions"] = dt;
        LoadExtentions();
    }
}